<?php
/**
 *
 *
 *
 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台控制器演示。
 *
 **/
namespace Home\Controller;

use Common\Model\OrderModel;
use Common\Model\OrderUpdateModel;
use Vendor\Page;

class IndexController extends ComController
{
    public function index()
    {
        $this->display();
    }

    /**
     * 新增工单
     */
    public function addOrder()
    {
        if (IS_POST) {
            $data['source'] = I('source', '其他');
            $data['game_user'] = I('game_user');
            if (empty($data['game_user'])) $this->error('游戏账号不能为空');
            $data['mobile'] = I('mobile');
            $data['user_remark'] = I('user_remark');
            $data['order_no'] = OrderModel::buildOrderNo();
            $data['create_date'] = date('Y-m-d H:i:s');
            $data['status'] = OrderModel::ADD_STATUS;
            $data['is_del'] = OrderModel::NOT_DELETE;
            $data['is_aftersale'] = I('is_aftersale', '2');
            $OrderModel = new OrderModel();
            //判断是否有存在ID
            $orders = $OrderModel->where(
                array(
                    'mobile' => $data['mobile'],
                    'game_user' => $data['game_user'],
                    'create_date' => array('gt', date('Y-m-d 00:00:00')),
                    'status' => array('lt', OrderModel::CLOSE_STATUS)
                ))->select();
            if ($orders) {
                $this->error('存在手机和账户一致的工单', U('queryOrder', array('mobile' => $data['mobile'])));

            } else {
                $id = $OrderModel->add($data);
                ($id == false) ? $this->error('提交错误') : $this->success('提交成功', U('queryOrder', array('mobile' => $data['mobile'])));

            }
        }
    }

    /**
     * 弃用
     */
    public function info()
    {
        $id = I('id');
        $OrderModel = new OrderModel();
        $count = $OrderModel->where(
            array(
                'id' => array('lt', $id),
                'status' => array('lt', OrderModel::QUERY_STATUS),
                'create_date' => array('gt', date('Y-m-d 00:00:00'))
            )
        )->count();
        $this->assign('count', $count);
        $order = $OrderModel->where(array('id' => $id))->field('order_no, status')->find();
        $this->assign('order', $order);
        $this->display();
    }

    public function queryOrder()
    {
        $mobile = I('mobile', '');
        if ($mobile) {

            $OrderModel = new OrderModel();
            $order = $OrderModel
                ->where(
                    array(
                        'mobile' => $mobile,
                        'status' =>
                            array('neq', OrderModel::CLOSE_STATUS)
                    )
                )
                ->field('id,order_no,game_user,status')
                ->order('id desc')
                ->select();

            if ($order == false) $this->error('暂时没有查到处理中订单');
            foreach ($order as $k => $v) {
                $ADD_STATUS = "0";
                $ACTIVATE_STATUS = "0";
                if ($v['status'] < OrderModel::QUERY_STATUS) {

                    $ADD_STATUS = $OrderModel
                        ->where(array('id' => array('lt', $v['id']),
                            'status' => array('in', array(OrderModel::ADD_STATUS,)),
                            'create_date' => array('gt', date('Y-m-d 00:00:00'))
                        ))
                        ->count();
                    $ACTIVATE_STATUS = $OrderModel
                        ->where(array('id' => array('lt', $v['id']),
                            'status' => array('in', array(OrderModel::ACTIVATE_STATUS,)),
                            'create_date' => array('gt', date('Y-m-d 00:00:00'))
                        ))
                        ->count();

                }
                $order[$k]['ADD_STATUS'] = $ADD_STATUS;
                $order[$k]['ACTIVATE_STATUS'] = $ACTIVATE_STATUS;
            }
            $this->assign('order', $order);
            $this->assign('QUERY_STATUS', OrderModel::QUERY_STATUS);
            $this->display('info');
        } else {
            $this->display();
        }
    }


    public function addOrderUpdate()
    {
        if (IS_POST) {

            $data = I('');
            $orderUpdateModel = new OrderUpdateModel();
            $result = $orderUpdateModel->createOrderUpdate($data);

            if ($result == false){
                $this->error($orderUpdateModel->getError());
            }else{
                $this->success('提交成功', U('addOrderUpdate'));
            }

        } else {

            $this->display();
        }

    }
}